package com.six.apidemic.activity;/*
 *Create by Administrator
 *on 2022/4/19
 */

import static com.six.apidemic.httpapi.Constract.SUCC_CODE;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.lzy.okgo.callback.StringCallback;
import com.six.apidemic.R;
import com.six.apidemic.bean.PersonBean;
import com.six.apidemic.bean.RegisterCallbackBean;
import com.six.apidemic.databinding.ActivityPersonBinding;
import com.six.apidemic.httpapi.HTTPAPI;
import com.six.apidemic.util.GsonUtils;
import com.six.apidemic.util.LogUtils;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author Administrator
 */
public class PersonActivity extends BaseActivity implements View.OnClickListener {

    ActivityPersonBinding binding;
    String name;
    String personId ;
    TextView tvModuleToScan;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_person);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        name = getIntent().getStringExtra("module_name");
        if (!TextUtils.isEmpty(name)) {
            getPersonInformation(name);
        }
        tvModuleToScan = findViewById(R.id.tvModuleToScan);
        tvModuleToScan.setOnClickListener(this);
        binding.btModuleRegister.setOnClickListener(this);
    }

    public void getPersonInformation(String name){
        HTTPAPI.getInstance().getPersonInformation(name,new StringCallback(){
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("个人信息获取成功-->"+ s);
                PersonBean bean = GsonUtils.GsonToBean(s,PersonBean.class);
                if (bean.getCode() == SUCC_CODE) {
                    binding.setBean(bean.getResult().get(0));
                    personId = bean.getResult().get(0).getId();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtils.e("个人信息获取失败-->"+ e.getMessage());
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btModuleRegister){
            if (!TextUtils.isEmpty(personId)) {
                toChangeType(personId,"4");
            }
        }else if (id == R.id.tvModuleToScan){
            startActivity(new Intent(PersonActivity.this, ScanHistoryActivity.class).putExtra("module_personId",personId));
        }
    }

    /**
     * 修改状态
     * @param id
     * @param status
     */
    public void toChangeType(String id,String status){
        HTTPAPI.getInstance().toChangeType(id,status,new StringCallback(){
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("修改状态成功-->"+ s);
                RegisterCallbackBean bean = GsonUtils.GsonToBean(s,RegisterCallbackBean.class);
                if (bean.getCode() == SUCC_CODE) {
                    showToast(bean.getMessage());
                    finish();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtils.e("修改状态失败-->"+ e.getMessage());
            }
        });
    }
}
